<?php 
/*
 * $HeadURL$
 * $Author$
 * $Revision$
 * $Date$
 *
 * ====================================================================
 *
 * Copyright (C) 2008 by loveapple.sourceforge.jp
 *
 * All copyright notices regarding loveapple and loveapple CoreLib
 * MUST remain intact in the scripts, documents and source code.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Correspondence and Marketing Questions can be sent to:
 * info at loveapple
 *
 * @author: loveapple
 */

require_once 'Zend/Log.php';
require_once 'Zend/Log/Writer/Db.php';
require_once 'Zend/Db.php';

/**
 * 
 * @author loveapple
 * @version $Revision$
 * @date $Date$
 * @id $Id$
 *
 */
class Third_Logger {
	private static $_logger;
	private static $_instance;
	private static $level;
	private $_ini;
	const DB_WRITER_SUFFIX = "DB_WRITER";
	const STREAM_WRITER_SUFFIX = "STREAM_WRITER";
	const FIREBUG_WRITER_SUFFIX = "FIREBUG_WRITER";
	const LOGGER_SUFFIX = "LOGGER";
	const CATEGORY = "CATEGORY";
	
	private function __construct() {
	}
	protected function init($ini = "Logger/Logger.ini"){
		$this->_ini = $this->parseIni(parse_ini_file($ini));
	}
	protected function parseIni(array $ini){
		
	}
	/**
	 * 
	 * @return unknown_type
	 */
	public static function getLogger(){
		if(!isset(self::$_instance)){
			self::$_instance = new Third_Logger();
			// init
			self::$_logger = self::$_instance->createDBWriter();
			if(!isset(self::$level)){
				self::$level = Zend_Log::ERR;
			}
		}
		return self::$_logger;
	}
	/**
	 * 
	 * @return unknown_type
	 */
	private function createDBWriter(){
		$params = array ('host'     => 'localhost',
                 'username' => 'root',
                 'password' => 'root',
                 'dbname'   => 'socialbase');
    	$db = Zend_Db::factory('PDO_MYSQL', $params);
    	$columnMapping = array('timestamp' => 'timestamp',
    							'priorityName'=>'priorityName',
    							'priority' => 'priority',
    							'message' => 'message');
    	$writer = new Zend_Log_Writer_Db($db, 'log', $columnMapping);
    	return new Zend_Log($writer);
	}
	/**
	 * 
	 * @param $message
	 * @return unknown_type
	 */
	public function debug($message){
		if(!$this->isDebug()){
			return ;
		}
		self::$_logger->log($message, Zend_Log::DEBUG);
	}
	/**
	 * 
	 * @param $message
	 * @return unknown_type
	 */
	public function emerg($message){
		self::$_logger->log($message, Zend_Log::EMERG);
	}
	public function alert($message){
		self::$_logger->log($message, Zend_Log::ALERT);
	}
	public function crit($message){
		self::$_logger->log($message, Zend_Log::CRIT);
	}
	public function err($message){
		self::$_logger->log($message, Zend_Log::ERR);
	}
	public function warn($message){
		self::$_logger->log($message, Zend_Log::WARN);
	}
	public function notice($message){
		self::$_logger->log($message, Zend_Log::NOTICE);
	}
	public function info($message){
		self::$_logger->log($message, Zend_Log::INFO);
	}
	protected function isLevel($targetLevel){
		if(!is_numeric($targetLevel)){
			return false;
		}
		if($targetLevel <= self::$level){
			return ture;
		}
		return false;
	}
	public function isDebug(){
		return $this->isLevel(Zend_Log::DEBUG);
	}
	public function isInfo(){
		return $this->isLevel(Zend_Log::INFO);
	}
	public function isNotice(){
		return $this->isLevel(Zend_Log::NOTICE);
	}
	public function isEmerg(){
		return $this->isLevel(Zend_Log::EMERG);
	}
	public function isAlert(){
		return $this->isLevel(Zend_Log::ALERT);
	}
	public function isCrit(){
		return $this->isLevel(Zend_Log::CRIT);
	}
	public function isErr(){
		return $this->isLevel(Zend_Log::ERR);
	}
	public function isWarn(){
		return $this->isLevel(Zend_Log::WARN);
	}
	
}
?>